<?php

namespace app\admin\model;

use think\Model;


class Register extends Model
{

    // 表名
    protected $name = 'register';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'c_time';
    protected $updateTime = 'u_time';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'tripmode_text',
        'inout_text',
        'c_time_text'
    ];
    

    
    public function getTripmodeList()
    {
        return ['0' => __('Tripmode 0'), '1' => __('Tripmode 1'), '2' => __('Tripmode 2'), '3' => __('Tripmode 3'), '4' => __('Tripmode 4')];
    }

    public function getInoutList()
    {
        return ['1' => __('Inout 1'), '2' => __('Inout 2')];
    }


    public function getTripmodeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['tripmode']) ? $data['tripmode'] : '');
        $list = $this->getTripmodeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getInoutTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['inout']) ? $data['inout'] : '');
        $list = $this->getInoutList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['c_time']) ? $data['c_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function quarters()
    {
        return $this->belongsTo('Quarters', 'quarters_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function user()
    {
        return $this->belongsTo('User', 'uid', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
